package cn.com.ghy.android.widget.work;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.bean.WorkReportBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.NoteOpeService;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:工作报告模块
 * @author wuz  @date 2011-5-24 14:51:14
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class WorkReport extends Activity {

	private EditText txtTopic = null;
	private EditText txtWorkReportContent = null;
	private Spinner spiType = null;
	private Button btnOnSubmit = null;
	private ProgressDialog progressDialog = null;
	private Bundle bundle = null;

	private String topic = "";
	private String workReportContent = "";
	private WorkReportBean workReportBean = new WorkReportBean();
	private LoginInfoBean loginInfo = null;

	private HttpThread httpThread = null;

	private DisposeException disposeException = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.work_report);
		getSession();
		findView();
		init();
		addListener();
	}

	private void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		loginInfo = ((ComStaticApp) this.getApplication()).getLoginInfo();
	}

	private void findView() {
		txtTopic = (EditText) this.findViewById(R.id.txtTopic);
		txtWorkReportContent = (EditText) this.findViewById(R.id.txtWorkReportContent);
		spiType = (Spinner) this.findViewById(R.id.spiType);
		btnOnSubmit = (Button) this.findViewById(R.id.btnOnSubmit);
	}

	private void init() {
		String[] workReportType = { getResources().getString(R.string.work_report_type_1),
				getResources().getString(R.string.work_report_type_2),
				getResources().getString(R.string.work_report_type_3) };
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(WorkReport.this,
				android.R.layout.simple_spinner_item, workReportType);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiType.setAdapter(adapter);
	}

	private void addListener() {
		spiType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				;
				workReportBean.setNotetype((String) parent.getItemAtPosition(position));
			}

			public void onNothingSelected(AdapterView<?> parent) {
			}
		});

		btnOnSubmit.setOnClickListener(btnListner);
	}

	private View.OnClickListener btnListner = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.btnOnSubmit :
					if (!getData()) {
						return;
					}
					workReportBean.setTitle(topic);
					workReportBean.setContent(workReportContent);
					workReportBean.setCuser(loginInfo.getPernr());
					httpThread = new HttpThread(handler);
					httpThread.doStart(workReportBean);
					break;
			}
		}
	};

	private boolean getData() {
		topic = txtTopic.getText().toString().trim();
		if (null == topic || "".equals(topic)) {
			Toast.makeText(WorkReport.this, "请输入主题!", Toast.LENGTH_SHORT).show();
			return false;
		}
		workReportContent = txtWorkReportContent.getText().toString().trim();
		if (null == workReportContent || "".equals(workReportContent)) {
			Toast.makeText(WorkReport.this, "请输入正文!", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	private class HttpThread extends Thread {
		private Handler handle = null;
		private WorkReportBean workReportBean = null;

		public HttpThread(Handler hander) {
			handle = hander;
		}

		public void doStart(WorkReportBean workReportBean) {
			this.workReportBean = workReportBean;
			progressDialog = new ProgressDialog(WorkReport.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在请求数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}

		public void run() {
			Message message = null;
			try {
				String result = null;
				NoteOpeService noteOpeService = new NoteOpeService();
				result = noteOpeService.SetNote(workReportBean);
				progressDialog.dismiss();
				message = handle.obtainMessage();
				bundle = new Bundle();
				message.what = CommonStatic.STATE_NORMAL;
				bundle.putString("result", result);
				message.setData(bundle);
				handle.sendMessage(message);
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message m) {
			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					String result = m.getData().getString("result");
					if ("OK".equals(result)) {
						new AlertDialog.Builder(WorkReport.this).setTitle("提示信息")
								.setMessage("提交任务成功, 是否关闭此页面!")
								.setPositiveButton("确定", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dlg, int sumthin) {
										WorkReport.this.finish();
									}
								}).setNeutralButton("取消", null).show();
					} else {
						new AlertDialog.Builder(WorkReport.this).setTitle("提示信息")
								.setMessage(result).setNeutralButton("关闭", null).show();
					}
					break;
			}
		}
	};
}
